//
//  KKHomeViewController.swift
//  KKWeibo
//
//  Created by kassia on 16/7/9.
//  Copyright © 2016年 kassia. All rights reserved.
//

import UIKit
import SVProgressHUD

private let Cell = "cell"
class KKHomeViewController: KKVisitorViewController {

    //微博数据列表
    lazy var statusListViewModel: KKStatusListViewModel = KKStatusListViewModel()
    
    //下拉刷新控件
    lazy var pullUpData: UIActivityIndicatorView = {
    
        let indictorView = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.WhiteLarge)
        indictorView.color = UIColor.grayColor()
        
        return indictorView
    }()
    
    // 上拉刷新
    lazy var pullDownView: KKRefreshController = {
    
        let ctr = KKRefreshController()
        
        // 添加下拉刷新的点击事件
        ctr.addTarget(self, action: "pullDownRefresh", forControlEvents: .ValueChanged)
        
        return ctr
    }()
    
    // tipLable
    private lazy var tipLable: UILabel = {
        let label = UILabel(textColor: UIColor.whiteColor(), fontSize: 12)
        label.backgroundColor = UIColor.orangeColor()
        label.textAlignment = .Center
        
        // 默认情况下隐藏
        label.hidden = true
        return label
        
    }()
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
 
        if isLogin {
           setupUI()
        //加载数据
          loadstatus()
          setupTableView()
            
        } else {
        
            //未登录状态下的文字图图片.
           vistorView?.updataCostomerView(nil, imageName: nil)
        }
    }
    
    // 添加控件 设置约束
    private func setupUI() {
    
        guard let nav = self.navigationController else {
        
            return
        }
        nav.view.insertSubview(tipLable, belowSubview: nav.navigationBar)
        tipLable.frame = CGRectMake(0, 34, nav.navigationBar.weight, 30)
    
    }
    
    // 开始动画'
    private func stareAnimation(message: String) {
    
        if tipLable.hidden == false {
            
            return
        }
        
        tipLable.hidden = false // 显示lable
        
        tipLable.text = message // 显示文字
        
        UIView.animateWithDuration(1, animations: { () -> Void in
 
          self.tipLable.transform = CGAffineTransformMakeTranslation(0, self.tipLable.height)
            
            }) { (_) -> Void in
                
                UIView.animateWithDuration(1, animations: { () -> Void in
                    
                  self.tipLable.transform = CGAffineTransformIdentity // 回到初始位置
                    
                    }, completion: { (_) -> Void in
                        
                     self.tipLable.hidden = true // 隐藏
                })
        }
    
    }
    private func setupTableView() {
    
        //注册cell
        tableView.registerClass(KKStatusViewCell.self, forCellReuseIdentifier: Cell)
    
        tableView.separatorStyle = .None //  取消分割线
        
        tableView.rowHeight = UITableViewAutomaticDimension //  设置自动行高, 通过约束设置高度

        tableView.estimatedRowHeight = 200 //  预估行高越接近真实高度越好
        
        tableView.tableFooterView = pullUpData // 添加footView
        
        tableView.addSubview(pullDownView) // 添加视图
        
        pullUpData.sizeToFit() // 指定大小

    }
    
    // 下拉刷新
    @objc private func pullDownRefresh() {
    
        loadstatus() // 加载数据
    }
    // 结束刷新
    func endRefreshing () {
    
        // 结束刷新
        pullUpData.stopAnimating()
        
        pullDownView.endRefreshing()
    }
    
    // 加载数据
       func loadstatus() {

        //初始化viewmodel
        statusListViewModel.loadstatus(pullUpData.isAnimating()) { (isSucess,message) -> () in

            if !self.pullUpData.isAnimating() {
            
                self.stareAnimation(message)
      
            }
         // 结束刷新
         self.endRefreshing()
            
         if isSucess {
            
            self.tableView.reloadData()//刷新数据
        }
        
        }

    }
 
}
// MARK: - 数据源方法
extension KKHomeViewController {

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        return self.statusListViewModel.tableViewList.count ?? 0
    }

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCellWithIdentifier(Cell, forIndexPath: indexPath) as! KKStatusViewCell // 创建cell
        
        let statusViewModel = statusListViewModel.tableViewList[indexPath.row] //从数组中取值给model
        
        cell.statusViewModel = statusViewModel // 赋值
        
        cell.selectionStyle = .None // 设置没有选中颜色
        
        return cell
        
    }
    
//     将要显示那个cell
    override func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
        
        if indexPath.row == statusListViewModel.tableViewList.count - 1 && !pullUpData.isAnimating() {
        
            pullUpData.startAnimating()
        
            print("已经滚动到最后一个cell")
            
            loadstatus()
        }
        
    }
    
}




